package game.spritesheet;

import common.Rectangle;

import engine.video.blit.SpriteInfo;

public class RegularSpriteSheet implements SpriteSheet_I {
	public RegularSpriteSheet(SpriteInfo baseSprite, int rows, int cols) {
		_BaseSprite = baseSprite;
		Rows = rows;
		Cols = cols;
		RowHeight = baseSprite.Image.getHeight() / Rows;
		ColWidth = baseSprite.Image.getHeight() / Cols;
		SpriteCount = rows * cols;
		
		// Build sprite infos.
		SpriteInfo[] sprites = new SpriteInfo[rows * cols];
		int rHeight = baseSprite.Image.getHeight() / rows;
		int cWidth = baseSprite.Image.getWidth() / cols;
		int index = 0;
		Rectangle rect;
		for(int y = 0; y < rows; y++) {
			for(int x = 0; x < cols; x++) {
				rect = new Rectangle(x * cWidth, y * rHeight, cWidth, rHeight);
				sprites[index++] = new SpriteInfo(baseSprite.Image, baseSprite.Mask, baseSprite.Offset, rect);
			}
		}
		_Sprites = sprites;
	}
	
	public final int Rows;
	public final int Cols;
	public final int RowHeight;
	public final int ColWidth;
	public final int SpriteCount;
	private SpriteInfo _BaseSprite;
	private SpriteInfo[] _Sprites;
	
	@Override
	public int getSpriteCount() {
		return SpriteCount;
	}
	@Override
	public SpriteInfo getBaseSprite() {
		return _BaseSprite;
	}
	@Override
	public SpriteInfo getSprite(int index) {
		if(index >= 0 && index < SpriteCount) {
			return _Sprites[index];
		} else {
			return null;
		}
	}
	@Override
	public SpriteInfo[] enumerateSprites() {
		return _Sprites;
	}
}
